package cn.kgc.springday07.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/6/19
 * @Description: 通知类
 */
@Component
@Aspect   // 标识当前类是通知类
public class AllAdvice {

    // 声明切点 实现代码的复用 简化代码的维护
    //@Pointcut("execution(* cn.kgc.springday07.interfaces.TeacherLi.teach*(String))")
    @Pointcut("@annotation(cn.kgc.springday07.annotations.OperationLog)")
    public void pointcut(){};
    // 前置通知
    //@Before("execution(* cn.kgc.springday07.interfaces.TeacherLi.teach*(..))")
    public void before(){
        System.out.println("--------------上课时间到了----------------");
    }

    // 后置通知
    //@AfterReturning("execution(* cn.kgc.springday07.interfaces.TeacherLi.teach*(..))")
    public void afterReturning(){
        System.out.println("--------------下课时间到了----------------");
    }


    //@After("execution(* cn.kgc.springday07.interfaces.TeacherLi.teach*(..))")
    public void after(){
        System.out.println("============最终通知===============");
    }

    //异常通知    异常通知和后置通知只能出现其一
    // @AfterThrowing("execution(* cn.kgc.springday07.interfaces.TeacherLi.teach*(..))")
    public void afterThrowing(){
         System.out.println("============异常通知===============");
    }

    //@Around("execution(* cn.kgc.springday07.interfaces.TeacherLi.teach*(..))")
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint joinPoint){


        try {
            System.out.println("--------------上课时间到了----------------");
            Object proceed = joinPoint.proceed();
            System.out.println("--------------下课时间到了----------------");
            return proceed;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

        return null;
    }

}
